import Vue from 'vue'
import Vuex from 'vuex'
import { getSession, setSession, removeSession } from "@/utils/session";
Vue.use(Vuex)

const state = {
    curKnoledge: JSON.parse(getSession("curKnoledge"))
}

const getters = {
    curKnoledge(state) {
        if (!state.curKnoledge) return;
        return state.curKnoledge;
    },
    knowledgeId(state) {
        if (!state.curKnoledge) return;
        return state.curKnoledge.id;
    },
    knowledgeName(state) {
        if (!state.curKnoledge) return;
        return state.curKnoledge.name;
    },
    knowledgeFiledId(state) {
        if (!state.curKnoledge) return;
        return state.curKnoledge.fileId;
    },
}

const actions = {
    setCourse({ commit }) {
        let curKnoledge = JSON.parse(getSession("curKnoledge"));
        commit('SETKNOLEDGE', curKnoledge)
    },
}

const mutations = {
    SETKNOLEDGE: (state, d) => {
        if (d) {
            setSession("curKnoledge", JSON.stringify(d))
        } else {
            removeSession("curKnoledge")
        }
        state.curKnoledge = d;
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}